package cn.xunjie.ymcc.service.impl;

import cn.xunjie.ymcc.domain.Login;
import cn.xunjie.ymcc.dto.LoginDto;
import cn.xunjie.ymcc.exception.GlobalException;
import cn.xunjie.ymcc.mapper.LoginMapper;
import cn.xunjie.ymcc.service.ILoginService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author xue.jiao
 * @since 2022-07-15
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public void loadByUsernameAndType(LoginDto dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        Integer type = dto.getType();
        //1.校验
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(type)){
            throw new GlobalException("参数必填！！");
        }
        //2.根据username + type 查询logininfo  要查询username phone email三个字段
        Login login = loginMapper.loadByUsernameAndType(username,type);
        //2.1.查询不到... 报错
        if(login == null){
            throw new GlobalException("账户不存在！！！");
        }
        //2.2 查到了，校验密码是否一致


        if(!password.equals(login.getPassword())){
            //3.密码错误，报错
            throw new GlobalException("密码错误！！！");
        }
        //4.登录成功


        /*//4.1 存储redis
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(
                token,
                logininfo,//对象要序列化
                30,
                TimeUnit.MINUTES
        );
        //4.2 返回token + logininfo
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        //我们是高级技师，应该考虑到一些敏感信息不要返回给前端
        logininfo.setSalt(null);
        logininfo.setPassword(null);
        map.put("logininfo",logininfo);
        return map;*/

    }
 }

